/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

var lastPoolId;
var lastTopicId;
var topics;
var nuggets;

function init() {
  lastPoolId = getCookie(taskNameShort+".qa.lastPoolId");
  lastTopicId = getCookie(taskNameShort+".qa.lastTopicId");
 
  document.title = 'SEPIA: '+msg( lang, 'msg_he_qa' );
  loadHeaderSubSub( lang );
  loadTopicList("QAEvalTopics");
  loadPoolList("QAEvalPools");
  loadMessages(lang);
  if ( lastTopicId ) {
  topicSelected(lastTopicId);
  }
}

function topicSelected(externalId) {
  if ( !externalId || externalId==-1 ) return;//in case blank selected
    $('main_block').style.display = 'none';
    
    var currentTopicIndex = 0; 
    for (var i = 0; i < topics.length; i++) {
      if (externalId == topics[i].externalId) {
        currentTopicIndex = i;
        break;
      }
    }
  loadTopicIntoHTML(topics[currentTopicIndex]);
  
  // load Nuggets
  var internalTopicId = topics[currentTopicIndex].topicId;
  loadNuggets(internalTopicId);
  
  lastTopicId = externalId;
  setCookie(taskNameShort+".qa.lastTopicId",lastTopicId);
  if ( lastPoolId ) {
    topicAndPoolSelected( lastTopicId, lastPoolId );
  }
}

function loadNuggets(internalTopicId) {
  var param = eval('$H({"action":"GetTopic", "topicId": ' + internalTopicId + ', "tool":"nv"})').toQueryString();  
  var json = sync(param);
  nuggets = json.topics[0].answerNuggets;
}

function topicAndPoolSelected(externalId, poolId) {
  if ( !externalId || externalId==-1 || !poolId || poolId==-1 ) return;//in case blank selected
  $('loading').style.display = '';    
  $('main_block').style.display = 'none';

  loadSystemResponses(externalId, poolId, "");

  lastPoolId = poolId;
  setCookie(taskNameShort+".qa.lastPoolId",lastPoolId);

  $('loading').style.display = 'none';
  $('main_block').style.display = '';
}

//This can be called from multiple functions
function loadSystemResponses(externalId, poolId, sortingCriteria) {
  var param = [];
  param.action = "QAEvalSystemResponses";
  param.poolId = poolId;
  param.externalId = externalId;
  param.sortingCriteria = sortingCriteria;
  var json = sync(param);
  var judgments = json.qa_judgments;
  if (!judgments) { return; }
  
  var html = "";
  for (var i = 0; i < judgments.length; i++) {
     var judgment = judgments[i];
     html += "<span class=\"header\">System Response "+(i+1)+"</span>\n";
    html += "<div class=\"green\">\n";
    html += judgment.systemResponse+"</div>\n";
    
    html += "<span class=\"header\">Answer Nuggets "+(i+1)+"</span>\n";
    html += "<div class=\"blue\">\n";
    
    var judgedNuggets = judgment.nuggets;
    var judgmentId = judgment.judgmentId;
    var counter = 0;
    for ( var j=0; j<nuggets.length; j++ ) {
      var nugget = nuggets[j];
      if (nugget.isSimilar==1) {
      continue;
      }
      var nuggetId = nugget.nuggetId;
      var nuggetText = nugget.answerNugget;
      var matched = 0;
      //inefficient. TODO: use hash
      for (var k=0; k<judgedNuggets.length; k++) {
      if ( judgedNuggets[k].nuggetId == nuggetId ) {
          if (judgedNuggets[k].matched == 1) {
          matched = 1;
          }
        break;
      }
      }
      html += "<div style=\"padding:1px;background-color:"+((counter++%2==1)?'#95FFD5':'#D5FFEE"')+"\">";
      html += "<input type=\"checkbox\" ";
      html += " id=\"ch"+judgmentId+"."+nuggetId+"\" "+(matched?"checked":"");
      html += " onclick=\"saveMatching('"+judgmentId+"',"+nuggetId+","+userId+",this.checked)\">";
      html += " &nbsp;";
      html += "<label for=\"ch"+judgmentId+"."+nuggetId+"\">"+nuggetText+"</label></div>\n";
    } 
    html += "</div>\n";
    html += "<br><br>\n"; 
  }
  $('nuggetMatchingArea').innerHTML = html;
}

function saveMatching(judgmentId, nuggetId, userId, matched) {
  var param = [];
  param.action = "QAEvalUpdate";
  param.judgmentId = judgmentId;
  param.nuggetId = nuggetId;
  param.userId = userId;
  param.matched = (matched?1:0);
  sync(param);
  showMessage( "Saved nugget judgment" );
}

function judgmentToDone( j ) {
  var done = "";
  if (j.judgment) {
    done += "["+j.judgment+"] ";
  }
  if ( j.evaluated_by_others > 0 ) {
    done += "[x] ";
  }
  return done;
}
